package com.fg.cloud.factory.controller.agency;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.ServiceException;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.reflection.ParamsReflect;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.agency.AgencyLevelEntity;
import com.fg.cloud.manager.service.agency.AgencyLevelService;

/**
 * 经销商-地址controller
 * @author around
 * @date 2017-6-22
 */
@Controller
@RequestMapping("/agencyLevel")
public class AgencyLevelController extends BaseController {

	@Autowired
	private AgencyLevelService agencyLevelService;
	
	/**
	 * 详情页信息封装查询
	 * @param request
	 * @param session
	 * @param level
	 * @return String 
	 */
	@RequestMapping(value = "/detail", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public void openPage(HttpServletRequest request, HttpServletResponse response, 
			AgencyLevelEntity level) {
		AgencyLevelEntity entity = agencyLevelService.find(level.getId());
		if(entity == null)
			doResult(response, false, StatusCode.FINDERROR, null, ResultType.FIND_NULL);
		else
			doResult(response, true, StatusCode.FINDSUCCESS, entity, ResultType.FIND_SUCCESS);
	}
	
	/**
	 * 分页查询 确认经销商对应的地址
	 * @param request
	 * @param session
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param agency
	 */
	@RequestMapping(value = "/findList", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public void findList(HttpServletRequest request, HttpServletResponse response, 
			Integer page, Integer rows, String sort, String order,
			AgencyLevelEntity agency) {
		PageUtils<AgencyLevelEntity> pageinfo = new PageUtils<AgencyLevelEntity>(page, rows, sort, order); 
		try {
			Map<String, Object> condition = ParamsReflect.getParamsByReflect(agency);
			condition.put(" facId =", getCurrentFactoryUser().getFacId());

			pageinfo.setCondition(condition);
			agencyLevelService.findList(pageinfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(response, false, StatusCode.FINDERROR, null, e.getMessage());
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/select", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public void findListBySelect(HttpServletRequest request, HttpServletResponse response) {
		try {
			doResult(response, true, StatusCode.FINDSUCCESS, 
					agencyLevelService.findBySelect(getCurrentFactoryUser().getFacId()), 
					ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			doResult(response, false, StatusCode.FINDERROR, null, e.getMessage());
			e.printStackTrace();
		}
	}
	
	/**
	 * 新增/保存厂商用户信息
	 * @param request
	 * @param response
	 * @param level AgencyLevelEntity
	 */
	@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
	@ResponseBody
	public void saveOrUpdate(HttpServletRequest request, HttpServletResponse response, 
			AgencyLevelEntity level) {
		try {
			boolean flag = agencyLevelService.verifyUniquely(level.getId(), 
					null == level.getFacId() ? getCurrentFactoryUser().getFacId() : 
						level.getFacId(), 
						level.getName());
			if (!flag) {
				doResult(response, false, StatusCode.SAVEERROR, null, "重复的等级名称");
				return;
			}
			
			setUpdateInfo(level, request);
			//id为空新增、不为空保存
			if(null == level.getId()) {
				level.setFacId(getCurrentFactoryUser().getFacId());
				setCreateInfo(level, request);
				flag = agencyLevelService.insertSelective(level);
			} else {
				flag = agencyLevelService.updateByPrimaryKeySelective(level);
			}
			doResult(response, flag, flag ? StatusCode.SAVESUCCESS : StatusCode.SAVEERROR, 
					null, flag ? ResultType.SAVE_SUCCESS : ResultType.SAVE_FAULT);
		} catch (org.springframework.dao.DuplicateKeyException e) {
			doResult(response, false, StatusCode.SAVEERROR, null, "存在重复的数据");
			e.printStackTrace();
		} catch (ServiceException e) {
			doResult(response, false, StatusCode.SAVEERROR, null, e.getMessage());
			e.printStackTrace();
		} 
	}
	
	/**
	 * 删除信息
	 * @param request
	 * @param response
	 * @param ids String => x,x,x
	 * @throws IOException 
	 */
	@RequestMapping(value = "/deletes", method = {RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public void deletes(HttpServletRequest request, HttpServletResponse response, 
			String ids) throws IOException {
		try {
			if(StringUtils.isEmpty(ids) || ids.split(",").length == 0) {
				doResult(response, false, StatusCode.REMOVEERROR, null, "请选择待删除记录");
				return;
			}
			boolean flag = agencyLevelService.deletes(getCurrentFactoryUser().getId(),
					getCurrentFactoryUser().getFacId(), 
					LongUtils.convert(ids.split("[, ]+")));
			
			doResult(response, flag, flag ? StatusCode.REMOVESUCCESS:StatusCode.REMOVEERROR, 
					null, flag ? ResultType.REMOVE_SUCCESS : ResultType.REMOVE_FAULT);
		} catch (Exception e) {
			doResult(response, false, StatusCode.REMOVEERROR, null, e.getMessage());
			e.printStackTrace();
		}
		
		
	}

}
